/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.refinedclasses;

import java.util.HashMap;
import org.orm.PersistentException;
import org.testis.orm.classes.T_test_designparttype;
import org.testis.orm.classes.T_test_designtree;
import org.testis.orm.classes.T_test_designtree_parameter_values;
import org.testis.orm.classes.T_test_designtree_parameters;
import org.testis.orm.classes.T_test_designtree_parametersCriteria;
import org.testis.utils.middleware.TestDesign;

/**
 * 
 * @author nurhat
 */
public class TestDesignTreeRefined {

    public enum TestDesignTreeParameters {
	// runtime parameters

	PcId, NicId, DeviceIp, LanguageId, ProfileId, TestBedId, DeviceId, OwnerId, BrowserType, BrowserSpeed, Timeout, ThreadPoolSize, InvocationCount, Critical, NoOfRetryOnFail, DataArrayAppliestoCase, AsynchCall, DelayTime, SuccessPercentage, RunAlways, ExpectedResultTypeId, FailOnPercantage, AfterDelayTime
    }

    private T_test_designtree m_reference;
    private static HashMap<Integer, TestDesignTreeRefined> m_nodeList = new HashMap<Integer, TestDesignTreeRefined>();
    private static HashMap<String, T_test_designtree_parameters> m_parameterDefs = new HashMap<String, T_test_designtree_parameters>();

    private TestDesignTreeRefined(T_test_designtree reference) {
	setTreeReference(reference);
	m_nodeList.put(reference.getId(), this);
    }

    public static TestDesignTreeRefined getDesignTree(
	    T_test_designtree reference) {
	if (m_nodeList.containsKey(reference.getId())) {
	    return m_nodeList.get(reference.getId());
	}
	TestDesignTreeRefined node = new TestDesignTreeRefined(reference);
	return node;
    }

    public void setTreeReference(T_test_designtree reference) {
	if (reference != null) {
	    m_reference = reference;
	} else {
	    throw new NullPointerException();
	}
    }

    public T_test_designtree getTreeReference() {
	return m_reference;
    }

    public T_test_designparttype getType() {
	return TestDesign.getDesignPartTypeById(m_reference.getTypeId());
    }

    public int getId() {
	return getTreeReference().getId();
    }

    public boolean isClone() {
	if (getTreeReference().getClone() == null) {
	    return false;
	} else {
	    return true;
	}
    }

    public boolean isGhost() {
	if (getTreeReference().getParent() == null
		&& getTreeReference().getRank() == null
		&& !TestDesign.getDesignPartTypeName(
			getTreeReference().getTypeId()).equals("TestSuite")) {
	    return true;
	} else {
	    return false;
	}
    }

    public T_test_designtree getOriginalTreeReference() {
	if (m_reference.getClone() != null) {
	    return m_reference.getClone();
	} else {
	    return m_reference;
	}
    }

    public void setTimeout(int value) {
	setTimeout(new Integer(value));
    }

    public T_test_designtree_parameters getParamByName(String paramName) {
	if (m_parameterDefs.containsKey(paramName)) {
	    return m_parameterDefs.get(paramName);
	}
	T_test_designtree_parametersCriteria criteria;
	T_test_designtree_parameters parameter = null;
	try {
	    criteria = new T_test_designtree_parametersCriteria();
	    criteria.name.eq(paramName);
	    parameter = criteria.uniqueT_test_designtree_parameters();
	} catch (PersistentException ex) {
	}
	if (parameter != null) {
	    m_parameterDefs.put(paramName, parameter);
	}
	return parameter;
    }

    public T_test_designtree_parameter_values getParamValue(
	    T_test_designtree_parameters param) {
	if (param == null) {
	    return null;
	}
	T_test_designtree_parameter_values parameterValue = null;
	org.testis.orm.classes.T_test_designtree_parameter_values[] lT_test_designtree_parameter_valuess = m_reference.t_test_designtree_parameter_values
		.toArray();
	for (int i = 0; i < lT_test_designtree_parameter_valuess.length; i++) {
	    if (lT_test_designtree_parameter_valuess[i].getParamId() == param
		    .getId()) {
		parameterValue = lT_test_designtree_parameter_valuess[i];
		break;
	    }
	}
	if ((parameterValue == null || parameterValue.getValue() == null)
		&& isClone()) {
	    lT_test_designtree_parameter_valuess = getOriginalTreeReference().t_test_designtree_parameter_values
		    .toArray();
	    for (int i = 0; i < lT_test_designtree_parameter_valuess.length; i++) {
		if (lT_test_designtree_parameter_valuess[i].getParamId() == param
			.getId()) {
		    parameterValue = lT_test_designtree_parameter_valuess[i];
		    break;
		}
	    }
	}
	return parameterValue;
    }

    public T_test_designtree_parameter_values getParamValue(
	    T_test_designtree_parameters param, boolean toSet) {
	if (param == null) {
	    return null;
	}
	boolean isRuntimeParameter = param.getIsRuntimeParameter();
	T_test_designtree_parameter_values parameterValue = null;
	org.testis.orm.classes.T_test_designtree_parameter_values[] lT_test_designtree_parameter_valuess = m_reference.t_test_designtree_parameter_values
		.toArray();
	for (int i = 0; i < lT_test_designtree_parameter_valuess.length; i++) {
	    if (lT_test_designtree_parameter_valuess[i].getParamId() == param
		    .getId()) {
		parameterValue = lT_test_designtree_parameter_valuess[i];
		break;
	    }
	}
	if (isRuntimeParameter && parameterValue == null) {
	    parameterValue = addParameter(param, null, getTreeReference());
	}
	if (!isRuntimeParameter && isClone()) {
	    lT_test_designtree_parameter_valuess = getOriginalTreeReference().t_test_designtree_parameter_values
		    .toArray();
	    for (int i = 0; i < lT_test_designtree_parameter_valuess.length; i++) {
		if (lT_test_designtree_parameter_valuess[i].getParamId() == param
			.getId()) {
		    parameterValue = lT_test_designtree_parameter_valuess[i];
		    break;
		}
	    }
	    if (parameterValue == null) {
		parameterValue = addParameter(param, null,
			getOriginalTreeReference());
	    }
	}
	return parameterValue;
    }

    public T_test_designtree_parameter_values addParameter(
	    T_test_designtree_parameters param, Object value,
	    T_test_designtree reference) {
	T_test_designtree_parameter_values paramValue = new T_test_designtree_parameter_values();
	paramValue.setDesignTree(reference);
	paramValue.setParamId(param.getId());
	if (value == null) {
	    paramValue.setValue(null);
	} else {
	    paramValue.setValue(value.toString());
	}
	try {
	    paramValue.save();
	    reference.t_test_designtree_parameter_values.add(paramValue);
	} catch (PersistentException ex) {
	}
	return paramValue;
    }

    public void removeParameter(T_test_designtree_parameters param) {
	T_test_designtree_parameter_values parameterValue = getParamValue(param);
	if (parameterValue != null) {
	    try {
		parameterValue.delete();
		m_reference.t_test_designtree_parameter_values
			.remove(parameterValue);
	    } catch (PersistentException ex) {
	    }

	}
    }

    public void setParameterValue(String paramName, Object paramValue) {

	T_test_designtree_parameters param = getParamByName(paramName);
	T_test_designtree_parameter_values parameterValue = getParamValue(
		param, true);
	if (parameterValue != null) {
	    if (paramValue == null) {
		parameterValue.setValue(null);
	    } else {
		parameterValue.setValue(paramValue.toString());
	    }

	    try {
		parameterValue.save();
	    } catch (PersistentException ex) {
	    }
	} else {
	    addParameter(param, paramValue, m_reference);
	}
    }

    public String getParameterValue(String paramName) {
	return getParameterStringValue(paramName);
    }

    public Integer getParameterIntValue(String paramName) {
	T_test_designtree_parameters param = getParamByName(paramName);
	T_test_designtree_parameter_values parameterValue = getParamValue(param);
	if (parameterValue != null) {
	    if (parameterValue.getValue() != null) {
		return Integer.parseInt(parameterValue.getValue());
	    }
	}
	return null;
    }

    public String getParameterStringValue(String paramName) {
	T_test_designtree_parameters param = getParamByName(paramName);
	T_test_designtree_parameter_values parameterValue = getParamValue(param);
	if (parameterValue != null) {
	    return parameterValue.getValue();
	}
	return null;
    }

    public Byte getParameterByteValue(String paramName) {
	T_test_designtree_parameters param = getParamByName(paramName);
	T_test_designtree_parameter_values parameterValue = getParamValue(param);
	if (parameterValue != null) {
	    if (parameterValue.getValue() != null) {
		return Byte.parseByte(parameterValue.getValue());
	    }
	}
	return null;
    }

    public Boolean getParameterBooleanValue(String paramName) {
	T_test_designtree_parameters param = getParamByName(paramName);
	T_test_designtree_parameter_values parameterValue = getParamValue(param);
	if (parameterValue != null) {
	    if (parameterValue.getValue() != null) {
		return Boolean.parseBoolean(parameterValue.getValue());
	    }
	}
	return false;
    }
}
